package com.manage.func.libmanagement.controller;

import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.manage.base.bean.CommonResultBean;
import com.manage.base.controller.BaseController;
import com.manage.config.model.Config;
import com.manage.func.libmanagement.model.LibManagementBean;
import com.manage.func.libmanagement.service.ILibManagementService;
import com.manage.user.model.User;

/**
 * 图书馆馆管理
 * @author EuroSun
 */
@Controller
@RequestMapping("/libmanagement")
public class LibManagementController extends BaseController {

	private static final long serialVersionUID = 1L;

	static final Logger LOG = LoggerFactory.getLogger(LibManagementController.class);

	@Autowired
	private ILibManagementService libManagementService;

	@RequestMapping(value = "select", method = RequestMethod.POST)
	@ResponseBody
	public CommonResultBean select(@RequestParam("data") String data) {
		CommonResultBean sys = new CommonResultBean();
		try {
			LOG.info("libManagement select called: " + data);
			LibManagementBean bean = fromJson(data, LibManagementBean.class);
			if (bean != null) {
				List<LibManagementBean> beans = libManagementService.selectLibManagementList(bean);
				int size = libManagementService.selectTotalCount(bean);
				sys.put("libManagement", beans);
				sys.put("size", size);
				return sys;
			}
			sys.setErrorMsg("请求参数为空");
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
			sys.setError();
		}
		return sys;
	}


	// 添加图书馆
	@RequestMapping(value = "add", method = RequestMethod.POST)
	@ResponseBody
	public CommonResultBean add(@RequestParam("data") String data, HttpServletRequest request) {
		CommonResultBean sys = new CommonResultBean();
		try {
			LOG.info("libManagement add called: " + data);
			User sysuser = (User) request.getSession().getAttribute(Config.USER_SESSION);
			if (null == sysuser) {
				sys.setErrorMsg("请先登录再进行操作");
				return sys;
			}
			LibManagementBean bean = fromJson(data, LibManagementBean.class);
			if (bean != null) {
				libManagementService.insertLibManagement(bean);
				sys.setResult_code(Config.SUCCESS);
				sys.setResult_message(Config.SUCCESS_MSG);
				return sys;
			}
			sys.setErrorMsg("请求参数为空");
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
			sys.setError();
		}
		return sys;
	}

	// 更新图书馆
	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	public CommonResultBean update(@RequestParam("data") String data, HttpServletRequest request) {
		CommonResultBean sys = new CommonResultBean();
		try {
			LOG.info("libManagement update called: " + data);
			User sysuser = (User) request.getSession().getAttribute(Config.USER_SESSION);
			if (null == sysuser) {
				sys.setErrorMsg("请先登录再进行操作");
				return sys;
			}
			LibManagementBean bean = fromJson(data, LibManagementBean.class);
			if (bean != null) {
				libManagementService.updateLibManagement(bean);
				sys.setResult_code(Config.SUCCESS);
				sys.setResult_message(Config.SUCCESS_MSG);
				return sys;
			}
			sys.setErrorMsg("请求参数为空");
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
			sys.setError();
		}
		return sys;
	}


	// 删除图书馆
	@RequestMapping(value = "delete", method = RequestMethod.POST)
	@ResponseBody
	public CommonResultBean delete(@RequestParam("data") String data) {
		LOG.info("LibManagement delete: " + data);
		CommonResultBean sys = new CommonResultBean();
		try {
			LibManagementBean bean = fromJson(data, LibManagementBean.class);
			// int i =
			libManagementService.deleteLibManagement(bean);
			sys.setResult_code(Config.SUCCESS);
			sys.setResult_message(Config.SUCCESS_MSG);
			return sys;
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
			sys.setError();
		}
		return sys;
	}
}
